﻿## 自定义类,仅适用于PowerShell5.0以上版本
class MyType
{
    [ValidateNotNullOrEmpty()][int]$Index;
    [ValidateNotNullOrEmpty()][float]$Height;
    [ValidateNotNullOrEmpty()][string]$Name;
}

function UserDefTypePrint([MyType]$VarIn)
{
    Write-Host "Index: "$VarIn.Index
    Write-Host "Height: "$VarIn.Height"(m)"
    Write-Host "Name: "$VarIn.Name
    Write-Host --------------------------------
}

function UserDefTypeSetVal([MyType]$VarIn)
{
    [MyType]$RstVar = New-Object -TypeName MyType;  #子函数类变量
    $RstVar.Index = $VarIn.Index;
    $RstVar.Height = $VarIn.Height;
    $RstVar.Name = $VarIn.Name;
    return $RstVar;  #函数返回值：类
}

################## 自定义类赋值测试 ###################
$VAR = [MyType]@{ Index=7; Height=1.73; Name="Jinquan" }  #类变量赋值
UserDefTypePrint($VAR)
$RST_VAR = UserDefTypeSetVal($VAR)
UserDefTypePrint($RST_VAR)